package com.fourforfo.fourmall.coupon.service.impl;

import com.fourforfo.fourmall.coupon.dao.SeckillSkuRelationDao;
import com.fourforfo.fourmall.coupon.entity.SeckillSkuRelationEntity;
import com.fourforfo.fourmall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fourforfo.common.utils.PageUtils;
import com.fourforfo.common.utils.Query;

import com.fourforfo.fourmall.coupon.dao.SeckillSessionDao;
import com.fourforfo.fourmall.coupon.entity.SeckillSessionEntity;
import com.fourforfo.fourmall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {


    @Autowired
    private SeckillSkuRelationDao seckillSkuRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     *    每天晚上3点，上架最近三天需要秒杀的商品
     *    当天：00:00:00 - 23:59:59
     *    明天：00:00:00 - 23:59:59
     *    后天：00:00:00 - 23:59:59
     * @return
     */
    @Override
    public List<SeckillSessionEntity> getLatest3DaySession() {
        LocalDate now = LocalDate.now(); //当天的年月日
        LocalDate thirdDay = now.plusDays(2); //当天的后天的年月日

        LocalTime min = LocalTime.MIN; //表示00:00:00
        LocalTime max = LocalTime.MAX; //表示23:59:599999

        LocalDateTime from = LocalDateTime.of(now, min); //当天 年月日 00:00:00
        LocalDateTime to = LocalDateTime.of(thirdDay, max); //后台 年月日 23:59:599999
        String start = from.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String end = to.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));


        List<SeckillSessionEntity> seckillSessionEntityList = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", start, end));

        if(seckillSessionEntityList==null||seckillSessionEntityList.size()==0){
            return seckillSessionEntityList;
        }

        List<Long> sessionIds = seckillSessionEntityList.stream().map(SeckillSessionEntity::getId).collect(Collectors.toList());

        List<SeckillSkuRelationEntity> entities = seckillSkuRelationDao.listBySessionIds(sessionIds);
        Map<Long, List<SeckillSkuRelationEntity>> entityMap = entities.stream().collect(Collectors.groupingBy(SeckillSkuRelationEntity::getPromotionSessionId));

        //获取该活动设计的商品项
        if(seckillSessionEntityList!=null&&seckillSessionEntityList.size()>0){
            for (SeckillSessionEntity seckillSessionEntity : seckillSessionEntityList) {
                Long sessionEntityId = seckillSessionEntity.getId();
                List<SeckillSkuRelationEntity> seckillSkuRelationEntities = entityMap.get(sessionEntityId);
                seckillSessionEntity.setRelationEntities(seckillSkuRelationEntities);
            }
        }

        return seckillSessionEntityList;
    }

}